from f5_driver.driver_v2 import F5DriverV2
from oslo_log import log as logging


LOG = logging.getLogger(__name__)


class Recreator(object):
    """ Make Driver RPC call"""

    def __init__(self, lbaas_plugin):
        self.lbaas_plugin = lbaas_plugin
        self.f5_driver = F5DriverV2(lbaas_plugin, 'Recreator')

    def recreate_loadbalancer(self, context, resource):
        try:
            LOG.info("start to recreate loadbalancer %s." % resource.id)
            self.f5_driver.loadbalancer.recreate(context, resource)
            LOG.info("finish to recreate loadbalancer %s." % resource.id)
        except Exception as exc:
            LOG.exception(
                "Can not recreate loadbalancer %s." %
                resource
            )
            LOG.exception(exc.message)

    def recreate_listener(self, context, resource):
        try:
            LOG.info("start to recreate listener %s." % resource.id)
            self.f5_driver.listener.recreate(context, resource)
            LOG.info("finish to recreate listencer %s." % resource.id)
        except Exception as exc:
            LOG.exception(
                "Can not recreate listener %s." %
                resource
            )
            LOG.exception(exc.message)

    def recreate_pool(self, context, resource):
        try:
            LOG.info("start to recreate pool %s." % resource.id)
            self.f5_driver.pool.recreate(context, resource)
            LOG.info("finish to recreate pool %s." % resource.id)
        except Exception as exc:
            LOG.exception(
                "Can not recreate pool %s." %
                resource
            )
            LOG.exception(exc.message)

    def recreate_member(self, context, resource):
        try:
            LOG.info("start to recreate member %s." % resource.id)
            self.f5_driver.member.recreate(context, resource)
            LOG.info("finish to recreate member %s." % resource.id)
        except Exception as exc:
            LOG.exception(
                "Can not recreate member %s." %
                resource
            )
            LOG.exception(exc.message)

    def recreate_healthmonitor(self, context, resource):
        try:
            LOG.info("start to recreate healthmonitor %s." % resource.id)
            self.f5_driver.healthmonitor.create(context, resource)
            LOG.info("finish to recreate healthmonitor %s." % resource.id)
        except Exception as exc:
            LOG.exception(
                "Can not recreate healthmonitor %s." %
                resource
            )
            LOG.exception(exc.message)

    def recreate_l7policy(self, context, resource):
        try:
            LOG.info("start to recreate l7policy %s." % resource.id)
            self.f5_driver.l7policy.recreate(context, resource)
            LOG.info("finish to recreate l7policy %s." % resource.id)
        except Exception as exc:
            LOG.exception(
                "Can not recreate l7policy %s" %
                resource
            )
            LOG.exception(exc.message)

    def recreate_l7rule(self, context, resource):
        pass
